package com.cyandpos.pad.codedcoupon.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.cyandpos.pad.R;
import com.cyandpos.pad.posdata.CodeCoupon;
import com.cyandpos.pad.utils.MoneyUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 20190920
 * 编码券adapter
 */
public class CodedCouponAdapter extends BaseAdapter {

    private Context mContext;
    private List<CodeCoupon> mData;//刷了几张卡的列表

    public CodedCouponAdapter(Context context, List<CodeCoupon> data ) {
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public CodeCoupon getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder ;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_coded_coupon, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvCodedCouponName.setText(getItem(position).getCodeCouponName());
        holder.tvCodedCouponCode.setText(getItem(position).getCouponCode());
        holder.tvCodedCouponValidDate.setText(getItem(position).getValidDate());

        int itemBalance = MoneyUtils.getMoneyInt(getItem(position).getBalance());
        int itemLimitM = MoneyUtils.getMoneyInt(getItem(position).getLimitMoney());

        int canUseMoney = itemBalance < itemLimitM ? itemBalance:itemLimitM;
        holder.tvCodedCouponLimitMoney.setText(MoneyUtils.getMoneyStr(canUseMoney)+ "");//允许金额
        holder.tvCodedCouponBalance.setText(MoneyUtils.getMoneyStr(itemBalance));//余额
        holder.tvCodedCouponPayed.setText(MoneyUtils.getMoneyStr(getItem(position).getUsedMoney()));//使用金额
        if (position % 2 == 0) {
            convertView.setBackgroundResource(R.mipmap.yhq_even);
        } else {
            convertView.setBackgroundResource(R.mipmap.yhq_odd);
        }
        return convertView;
    }

    public static class ViewHolder {
        @BindView(R.id.tv_coded_coupon_name)
        TextView tvCodedCouponName;
        @BindView(R.id.tv_coded_coupon_code)
        TextView tvCodedCouponCode;
        @BindView(R.id.tv_coded_coupon_valid_date)
        TextView tvCodedCouponValidDate;
        @BindView(R.id.tv_coded_coupon_limit_money)
        public TextView tvCodedCouponLimitMoney;
        @BindView(R.id.tv_coded_coupon_balance)
        public TextView tvCodedCouponBalance;
        @BindView(R.id.tv_coded_coupon_payed)
        public TextView tvCodedCouponPayed;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
